<template>
  <el-row
    type="flex"
    justify="space-between"
    align="middle"
    style="width:100%;height: 40px"
  >
    <el-col>
      <strong v-if="isRoot"> <i class="el-icon-s-home" /> {{ treeNode.name }}</strong>
      <span v-else>{{ treeNode.name }}</span>
    </el-col>
    <el-col :span="4">
      <el-row type="flex" justify="end">
        <!-- 两个内容 -->
        <el-col>{{ treeNode.manager }}</el-col>
        <el-col>
          <!-- 下拉菜单 element -->
          <el-dropdown :trigger="isRoot ? 'click' : 'hover'">
            <span> 操作<i class="el-icon-arrow-down" /> </span>
            <!-- 下拉菜单 -->
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="addDeparment">添加子部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" @click.native="editDeparment">编辑部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" @click.native="delDeparments(treeNode.id)">删除部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delDeparments } from '@/api/departments'
export default {
  props: {
    // eslint-disable-next-line vue/require-default-prop
    treeNode: {
      type: Object,
      require: true
    },
    isRoot: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    async delDeparments(id) {
      try {
        await this.$confirm('删除后将无法恢复！', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        await delDeparments(id)
        this.$message({
          type: 'success',
          message: '删除成功'
        })
        this.$emit('delDeparments')
      } catch (error) {
        console.log(error)
      }
    },
    addDeparment() {
      this.$emit('addDeparment', this.treeNode)
    },
    editDeparment() {
      this.$emit('editDeparment', this.treeNode)
    }
  }
}
</script>

<style>

</style>
